﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKStairs.CommonUnit
{
	public class LinkElement
	{
		public LinkElement(Autodesk.Revit.DB.Document docRootPar, List<RevitLinkInstance> InsLinksPar, ElementId IdSelf)
		{
			this.m_DocRoot = docRootPar;
			this.m_InsLinks = InsLinksPar;
			this.m_ElementIdSelf = IdSelf;
			this.m_DocCurrent = this.m_DocRoot;
			int count = InsLinksPar.Count;
			if (InsLinksPar.Count > 0)
			{
				foreach (RevitLinkInstance revitLinkInstance in InsLinksPar)
				{
					this.m_LinkTransForm = this.m_LinkTransForm.Multiply(revitLinkInstance.GetTransform());
				}
				this.m_DocCurrent = InsLinksPar[count - 1].GetLinkDocument();
			}
		}

		public ElementId CopyElementToDocRoot()
		{
			if (this.m_DocCurrent == this.m_DocRoot)
			{
				return this.m_ElementIdSelf;
			}
			CopyPasteOptions copyPasteOptions = new CopyPasteOptions();
			copyPasteOptions.SetDuplicateTypeNamesHandler(new HideAndAcceptDuplicateTypeNamesHandler());
			List<ElementId> list = new List<ElementId>();
			list.Add(this.m_ElementIdSelf);
			return ElementTransformUtils.CopyElements(this.m_DocCurrent, list, this.m_DocRoot, this.m_LinkTransForm, copyPasteOptions).First<ElementId>();
		}

		public Element GetSelfElement()
		{
			return this.m_DocCurrent.GetElement(this.m_ElementIdSelf);
		}

		public RevitLinkInstance GetParentLinkInstance()
		{
			if (this.m_InsLinks.Count == 0)
			{
				return null;
			}
			return this.m_InsLinks[this.m_InsLinks.Count - 1];
		}

		private Document m_DocRoot;

		private Transform m_LinkTransForm = Transform.Identity;

		private List<RevitLinkInstance> m_InsLinks = new List<RevitLinkInstance>();

		private ElementId m_ElementIdSelf;

		private Document m_DocCurrent;
	}
}
